package com.doge.walknovel.business.novel.chapter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.doge.wnpro.R
import com.doge.walknovel.api.model.NovelDetailInfo
import com.doge.walknovel.api.model.ChapterInfo
import com.doge.walknovel.business.novel.NovelRefer
import com.doge.walknovel.business.reader.ReaderActivity
import com.doge.walknovel.core.BaseActivity
import com.doge.walknovel.core.base.adapters.ITEM_CLICKED
import com.doge.walknovel.core.base.isRefreshing
import com.doge.wnpro.databinding.ActivityNovelChapterBinding
import com.doge.walknovel.utils.Activities

class NovelChapterActivity : BaseActivity() {
    private val binding by lazy { ActivityNovelChapterBinding.inflate(layoutInflater) }
    private val referId: Int? by lazy { intent?.getIntExtra(NovelRefer.EXTRA_REFER_ID, 0) }
    private val refer by lazy { intent?.getStringExtra(NovelRefer.EXTRA_REFER)!! }
    private val novelId by lazy { intent?.getIntExtra(EXTRA_NOVEL_ID, 0)!! }
    private val totalChapterCount by lazy { intent?.getIntExtra(EXTRA_TOTAL_CHAPTER_COUNT, 0)!! }
    private val viewModel: NovelChapterViewModel by viewModels {
        NovelChapterViewModelFactory(novelId)
    }
    private val adapter by lazy { NovelChapterAdapter() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.total.text =
            String.format(getString(R.string.xxx_chapters), totalChapterCount.toString())
        binding.recyclerView.itemAnimator = null
        binding.recyclerView.layoutManager = LinearLayoutManager(binding.recyclerView.context)
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        binding.refreshLayout.setColorSchemeResources(R.color.pull_refresh)
        binding.refreshLayout.setOnRefreshListener {
            viewModel.request()
        }
        viewModel.loading.observe(this) {
            binding.refreshLayout.isRefreshing = it.isRefreshing()
        }
        viewModel.dataList.observe(this) {
            adapter.submitList(it)
        }
        viewModel.request()
    }

    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            ITEM_CLICKED -> {
                ReaderActivity.start(this, novelId, (item as? ChapterInfo?)?.chapterId, refer, referId)
                Activities.finish(NovelChapterActivity::class.java)
            }
        }
    }

    companion object {
        private const val EXTRA_NOVEL_ID = "novelId"
        private const val EXTRA_TOTAL_CHAPTER_COUNT = "totalChapterCount"
        fun start(context: Context?, data: NovelDetailInfo?, refer: String, referId: Int? = null) {
            val novelId = data?.novelId
            val totalChapterCount = data?.chapterCount
            if (context == null || novelId == null || totalChapterCount == null) return
            context.startActivity(Intent(context, NovelChapterActivity::class.java).apply {
                putExtra(EXTRA_NOVEL_ID, novelId)
                putExtra(EXTRA_TOTAL_CHAPTER_COUNT, totalChapterCount)
                putExtra(NovelRefer.EXTRA_REFER, refer)
                referId?.let { putExtra(NovelRefer.EXTRA_REFER_ID, it) }
            })
        }
    }
}